package cn.slf.ph.servlet;

import cn.slf.ph.dao.CourierStationDao;
import cn.slf.ph.dao.impl.CourierStationDaoImpl;
import cn.slf.ph.entity.Community;
import cn.slf.ph.entity.CourierStation;
import cn.slf.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

@WebServlet("/courierstation")
public class CourierStationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取action判断接下来要进行的操作
        //接收action
        String action = req.getParameter("action");
        String action2 = req.getParameter("action2");
        CourierStationDao courierStationDao = new CourierStationDaoImpl();
        //进行判断
        if ("selectAll".equals(action)) {
            //获取数据
            String courierStationName = req.getParameter("courierStationName");
            String communityName = req.getParameter("communityName");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String courierStationStatus = req.getParameter("courierStationStatus");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            //封装map键值对集合，调用接口获得需要的数据
            Map<String, String> map = new HashMap<>();
            map.put("courierStationName", courierStationName);
            map.put("communityName", communityName);
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("courierStationStatus", courierStationStatus);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            //连接数据库
            List<CourierStation> list = courierStationDao.selectAll(map);
            //数据总条数
            int total = courierStationDao.selectCount(map);
            //给出响应信息
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("update".equals(action)) {
            //收集数据
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String Id1 = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId = Integer.parseInt(Id1);
            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String Id2 = req.getParameter("courierStationId");
            int courierStationId = Integer.parseInt(Id2);
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String id3 = req.getParameter("courierStationOrderCount");
            int courierStationOrderCount = Integer.parseInt(id3);
            String courierStationProvince = req.getParameter("courierStationProvince");
            String id4 = req.getParameter("courierStationSort");
            int courierStationSort = Integer.parseInt(id4);
            String id5 = req.getParameter("courierStationStatus");
            int courierStationStatus = Integer.parseInt(id5);
            //组成对象
            CourierStation courierStation = new CourierStation(courierStationId, courierStationName, courierStationImage, courierStationCommunityName, courierStationOrderCount, courierStationStatus, courierStationSort, courierStationProvince, courierStationDistrict, courierStationAddress, courierStationCity, courierStationCommunityId);
            int num = courierStationDao.update(courierStation);
            Result success = null;
            if (num != 0) {
                success = Result.success("修改成功");
            } else {
                success = Result.error("修改失败");
            }
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("insert".equals(action)) {
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
//            String courierStationCommunityName = req.getParameter("courierStationCommunityName");
            String Id = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId = Integer.parseInt(Id);

            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");
            //
            CourierStation courierStation = new CourierStation(courierStationName, courierStationImage, courierStationProvince, courierStationDistrict, courierStationAddress, courierStationCity, courierStationCommunityId);
            int num = courierStationDao.insert(courierStation);
            Result success = null;
            if (num != 0) {
                success = Result.success("添加成功");
            } else {
                success = Result.error("添加失败");
            }
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("modifyCourierStationStatus".equals(action)) {
            String courierStationId = req.getParameter("courierStationId");
            String courierStationStatus = req.getParameter("courierStationStatus");
            Map<String, String> map = new HashMap<>();
            map.put("courierStationId", courierStationId);
            map.put("courierStationStatus", courierStationStatus);
            //连接数据库
            int num = courierStationDao.ModifyCourierStationStatus(map);
            Result success = null;
            if (num != 0) {
                success = Result.success("修改成功");
            } else {
                success = Result.error("修改失败");
            }
            //给出响应
            resp.getWriter().write(JSON.toJSONString(success));
        }
//        if ("getcommunitylist".equals(action2)) {
//            String pageNum = req.getParameter("pageNum");
//            String pageSize = req.getParameter("pageSize");
//            Map<String, String> map = new HashMap<>();
//            map.put("pageNum", pageNum);
//            map.put("pageSize", pageSize);
//            //返回
//            List<Community> list = courierStationDao.getCommunityList(map);
//            int total = courierStationDao.communityCount(map);
//
//            Map resultMap = new HashMap();
//            resultMap.put("list", list);
//            resultMap.put("total", total);
//            Result success = Result.success(resultMap);
//        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
